package org.jslaughter.component.inventory;

import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.nvframe.component.AbstractComponent;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.UpdateEvent;
import org.nvframe.event.eventtype.UpdateListener;
import org.nvframe.exception.NVFrameException;
import org.nvframe.factory.EntityFactory;
import org.nvframe.manager.ConfigManager;
import org.nvframe.manager.InputManager;
import org.nvframe.util.settings.SettingsObj;

public class WeaponManager extends AbstractComponent implements UpdateListener {
	
	private Map<Integer, Weapon> weaponSlots;
	private Map<Integer, Integer> weaponSlotKey;
	private int activeSlotId;

	public WeaponManager(String id, Entity owner, SettingsObj settings) {
		super(id, owner);

		weaponSlots = new HashMap<Integer, Weapon>();
		weaponSlotKey = new HashMap<Integer, Integer>();
		activeSlotId = 1;
		
		String key = settings.getString("slot1_key", null);
		int slotId = 1;
		
		// try to search for more slots
		while(key != null) {			
			weaponSlots.put(slotId, null);
			weaponSlotKey.put(InputManager.getInstance().getInput(key), slotId);
			
			slotId++;
			key = settings.getString("slot"+slotId+"_key", null);
		}
		
		EventService.getInstance().addEventListener(this);
	}
	
	public void addWeapon(String id) {
		Weapon weapon;
		
		try {
			weapon = (Weapon) EntityFactory.getInstance().getEntityFromPrototype(id);
			
			// for later reference
			weapon.setOwner(owner);
			
			int slotId = weapon.getWeaponTargetSlot();
			
			weapon.setWeaponOwner(owner);
			
			// deactivate weapon at the target slot
			Weapon oldWeapon = weaponSlots.get(slotId);
			if(oldWeapon != null)
				oldWeapon.removed();
			
			// set new weapon active when this weapon's slot is the activeslot 
			if(slotId == activeSlotId)
				weapon.setEnabled(true);
			else
				weapon.setEnabled(false);

			// put weapon in correct slot
			weaponSlots.put(slotId, weapon);
		
		} catch (NVFrameException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpdate(UpdateEvent event) {
		GameContainer gc = event.getGc();
		
		Input input = gc.getInput();
				
		// check numeric keys
		for(Integer key : weaponSlotKey.keySet()) {
			if(!input.isKeyPressed(key))
				continue;
			
			Integer slotId = weaponSlotKey.get(key);
			
			Weapon newWeapon = weaponSlots.get(slotId);
			if(newWeapon == null) {
				System.out.println("no weapon in slot " + slotId);
				continue;
			}

			if(ConfigManager.getInstance().getActive("debugMode"))
				System.out.println("equiped weapon from slot " + slotId);
			
			// disactivate current active weapon
			Weapon currentWeapon = weaponSlots.get(activeSlotId);
			if(currentWeapon != null)
				currentWeapon.setEnabled(false);
			
			// activate new weapon
			newWeapon.setEnabled(true);
			
			activeSlotId = slotId;
		}
		
		// TODO: check mousescroll
	}
	
	@Override
	public void removed() {
		for(Entity weapon : weaponSlots.values())
			if(weapon != null)
				weapon.removed();
	}
	
}
